<?php
/**
 * | @author 韩威兵^life <1877361302@qq.com>
 * +----------------------------------------------------------------------
 * | @copyright 2016 - 2019 HQ
 * +----------------------------------------------------------------------
 * | @version $Id: 2019/4/27 17:44 jiahe_new Goods.php 韩威兵^life $
 * +----------------------------------------------------------------------
 */

namespace app\common\common\model\oldModel\goods;

use app\common\common\model\oldModel\OldBase;
use app\common\common\model\oldModel\store\StoreExt;

class  Goods extends OldBase
{
    //表名称
    protected $table = 'goods';
    //错误消息
    public $error = '';
    //状态值
    public $status = true;
    //其他附带信息
    public $data = [];
    //status状态值
    public static $status_on     = 1;
    public static $status_off    = 0;
    public static $status_disuse = 2;
    //is_meal状态值
    public static $meal_ok  = 1;
    public static $meal_off = 0;

    /**
     * 通过where条件查询数据
     * @param $where
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function allByWhere($where,$goods_field='*')
    {
        return $this->where($where)->field($goods_field)->select()->ToArray();
    }

    /**
     * @param $where
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function findByWhere($where)
    {
        return $this->where($where)->findToArray();
    }

    /**
     * 查找商品信息
     * @param $storeId
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function findStoreGoods($where)
    {


        $list = self::alias('g')->field('g.*,sg.goods_pic_name spic_name,sg.dkid')
            ->join('store_goods sg', 'sg.dishsno=g.did AND sg.old_dkid IS NULL')
            ->where($where)
            ->order(' sg.sort asc, g.goods_id desc')
            ->selectToArray();

        //todo aw:下面的可以单独令出去一个方法
        return $list;
    }

    /**
     * 首页需要的数据
     * @param $storeId
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function allStoreGoods($storeId)
    {
        $where['sg.store_id'] = $storeId;
        $where['sg.status']   = self::$status_on;
        $where['sg.is_meal']  = self::$meal_ok;

        $list = self::alias('g')->field('g.*,sg.member_price,sg.price,sg.current_price,sg.sort,sg.stock,sg.goods_pic_name spic_name,sg.dkid,sg.goods_pic spic,sg.is_meal,sg.is_takeout,sg.goods_type_id,gc.name gc_name,gc.sort c_sort,sg.second_half')
            ->join('store_goods sg', 'sg.dishsno=g.did AND sg.old_dkid IS NULL')
            ->join('goods_category gc','gc.category_id = sg.goods_type_id')
            ->where($where)
            ->where('g.old_did', 'null')
            ->group('g.did')
            ->order('gc.sort asc, sg.sort asc, g.goods_id desc')
            ->selectToArray();
        //todo aw:下面的可以单独令出去一个方法
        return $list;
    }

    /**
     * 首页需要的数据
     * @param $storeId
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function allStoreGoodsByDishsno($dishsnos, $sg_ids = '')
    {
        $where['sg.dishsno'] = array('in', $dishsnos);
        $where['sg.status']   = self::$status_on;
        $where['sg.is_meal']  = self::$meal_ok;
        if($sg_ids){
            $where['sg.id'] = array('in', $sg_ids);
        }
        $list = self::alias('g')->field('g.*,sg.recommend_sort,sg.id as iiid ,sg.member_price,sg.price,sg.current_price,sg.sort,sg.stock,sg.goods_pic_name spic_name,sg.dkid,sg.goods_pic spic,sg.is_meal,sg.is_takeout,sg.goods_type_id,gc.name gc_name,gc.sort c_sort,sg.second_half')
            ->join('store_goods sg', 'sg.dishsno=g.did AND sg.old_dkid IS NULL')
            ->join('goods_category gc','gc.category_id = sg.goods_type_id')
            ->where($where)
            ->where('g.old_did', 'null')
            ->group('g.did')
            ->order('gc.sort asc, sg.sort asc, g.goods_id desc')
            ->selectToArray();


        return $list;
    }

    /**
     * 获取商品信息
     * @param string $storeId  门店Id
     * @param string $dishsno  速记码
     * @oaram int  $type   订单类型（1：外卖；2：自取；3：堂食）
     */
    public function getGoodsInfo( $storeId =  0 , $dishsno = '' , $type = 1){

        if( $type == 1){
            $map['sg.is_takeout'] = 1;
        }
        if( $type == 3){
            $map['sg.is_meal'] = 1;
        }
        $map['sg.store_id'] = $storeId;
        //$map['sg.status']   = self::$status_on;
        $map['sg.dishsno']  = $dishsno;
        $info = $this->where( $map )->alias( 'g')
            ->join('store_goods sg', 'sg.dishsno=g.did')
            ->field('g.*,sg.member_price,sg.price,sg.current_price,sg.dkid , sg.dishsno,sg.id as store_goods_id , sg.second_half , sg.stock,sg.status')
            ->find();

        if( $info ){
            $info['member_price'] =  $info['member_price'] ? $info['member_price'] : $info['current_price'];
        }
        return $info;
    }
}